
package cn.net.greenet.scheduler;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import cn.net.greenet.db.bean.Messageincominglog;
import cn.net.greenet.db.bean.Versionreported;
import cn.net.greenet.policysyn.ReportedVersionNormalizer;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author hedingwei
 */
public class ReportedVersionScheduler {

    public static ReportedVersionScheduler instance = null;

    private Timer timer = new Timer("ReportedVersionScheduler");

    private ReportedVersionScheduler() {
    }

    public static ReportedVersionScheduler getInstance() {
        if (instance == null) {
            instance = new ReportedVersionScheduler();
        }
        return instance;
    }

    public void start() {
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                Session session = null;
                try {
                    SessionFactory sf = HibernateUtil.getSessionFactory();
                    sf.getCache().evictEntityRegion(Messageincominglog.class);
                    sf.getCache().evictEntityRegion(Versionreported.class);
                    sf.getCache().evictEntityRegion(Dpiendpointbean.class);
                    session = sf.openSession();
                    List<Dpiendpointbean> l = session.createQuery("from Dpiendpointbean").list();

                    for (Dpiendpointbean bean : l) {
                        try {
                            new ReportedVersionNormalizer().normalize(bean.getId(), session);
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (session != null) {
                        session.close();
                    }
                }

            }
        }, 10000, 60000);
    }
}
